package com.share.shareplatform.sharegoods.service.impl;


import com.share.shareplatform.pojo.GoodsOneComment;
import com.share.shareplatform.pojo.GoodsTwoComment;
import com.share.shareplatform.pojo.User;
import com.share.shareplatform.sharegoods.dao.GoodsOneCommentDao;
import com.share.shareplatform.sharegoods.dao.GoodsTwoCommentDao;
import com.share.shareplatform.sharegoods.service.GoodsCommentService;
import com.share.shareplatform.sharegoods.service.feign.UserService;
import com.share.shareplatform.util.CollectionUtil;
import com.share.shareplatform.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GoodsCommentServiceImpl implements GoodsCommentService {

    @Autowired
    private GoodsOneCommentDao goodsOneCommentDao;

    @Autowired
    private GoodsTwoCommentDao goodsTwoCommentDao;

    @Resource
    private UserService userService;

    @Override
    public Long addOneOne(GoodsOneComment goodsOneComment) {
        return goodsOneCommentDao.addOne(goodsOneComment);
    }

    @Override
    public Long addOneOTwo(GoodsTwoComment goodsTwoComment) {
        return goodsTwoCommentDao.addOne(goodsTwoComment);
    }

    @Override
    public Integer deleteOneOneById(Long id) {
        return goodsOneCommentDao.deleteOneById(id);
    }

    @Override
    public Integer deleteTwoOneById(Long id) {
        return goodsTwoCommentDao.deleteOneById(id);
    }

    @Override
    public Integer deleteAllOneByIds(Long[] ids) {
        int tol=0;
        for(Long id:ids){
            Integer flag = goodsOneCommentDao.deleteOneById(id);
            tol+=flag;
        }
        return tol;
    }

    @Override
    public Integer deleteAllTwoByIds(Long[] ids) {
        return goodsTwoCommentDao.deleteAllByIds(ids);
    }


    @Override
    public List<GoodsOneComment> findAllOneByGoodsId(Long id) {

        List<GoodsOneComment> commentList = goodsOneCommentDao.findAllByGoodsId(id);

        StringBuilder userBuilder=new StringBuilder();

        for(GoodsOneComment goodsOneComment: commentList){
            userBuilder.append(goodsOneComment.getUserId()+"-");
        }

        if(userBuilder.length()>0){
            userBuilder.deleteCharAt(userBuilder.length()-1);
            Map<Long, User> userMap = Result.getMapResultValue(userService.findAllSafeByIdsAsMap(userBuilder.toString()), Long.class, User.class);
            for(GoodsOneComment goodsOneComment:commentList){
                goodsOneComment.setUser(userMap.get(goodsOneComment.getUserId()));
            }
        }

        return commentList;

    }

    @Override
    public List<GoodsTwoComment> findAllTwoByGoodsIdAndReplyId(Long goodsId, Long replyId) {
        List<GoodsTwoComment> commentList = goodsTwoCommentDao.findAllByGoodsIdAndReplyId(goodsId, replyId);

        StringBuilder userBuilder=new StringBuilder();

        for(GoodsTwoComment goodsTwoComment: commentList){
            userBuilder.append(goodsTwoComment.getUserId()+"-");
            userBuilder.append(goodsTwoComment.getReplyUserId()+"-");
        }

        if(userBuilder.length()>0){
            userBuilder.deleteCharAt(userBuilder.length()-1);
            Map<Long, User> userMap = Result.getMapResultValue(userService.findAllSafeByIdsAsMap(userBuilder.toString()), Long.class, User.class);
            for(GoodsTwoComment goodsTwoComment: commentList){
                goodsTwoComment.setUser(userMap.get(goodsTwoComment.getUserId()));
                goodsTwoComment.setReplyUser(userMap.get(goodsTwoComment.getReplyUserId()));
            }
        }

        return commentList;
    }

    @Override
    public GoodsOneComment findOneOneById(Long id) {
        GoodsOneComment comment = goodsOneCommentDao.findOneById(id);

        User user = Result.getResultValue(userService.findOneSafeById(comment.getUserId()), "user", User.class);

        if(user!=null){
            comment.setUser(user);
        }

        return comment;
    }

    @Override
    public GoodsTwoComment findOneTwoById(Long id) {

        GoodsTwoComment comment = goodsTwoCommentDao.findOneById(id);
        User user = Result.getResultValue(userService.findOneSafeById(comment.getUserId()), "user", User.class);

        if(user!=null){
            comment.setUser(user);
        }
        return comment;
    }

    /*
    * 删除一级评论时将关联的二级评论也删除
    * */
    @Override
    public Integer deleteOneOneForeignById(Long id) {
        Integer flag = goodsOneCommentDao.deleteOneById(id);
        Integer flag2 = goodsTwoCommentDao.deleteOneByReplyId(id);

        return flag+flag2;
    }


}
